﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EIA.Basic.Model;
using EIA.Basic.DAL;
using System.Collections;

namespace EIA.Basic.BLL
{
    public class XStaffBlp : IXStaffBlp
    {
        ITStaffDao _TStaffDao = null;
        ITUserDao _TUserDao = null;

        public XStaffBlp()
        {
            _TStaffDao = X.DaoFactory.Create<ITStaffDao>();
            _TUserDao = X.DaoFactory.Create<ITUserDao>();
        }

        public int Insert(Tuple<TStaff, TUser> stf)
        {
            _TStaffDao.DASession.BeginTransaction();
            try
            {
                int stf_id = (int)_TStaffDao.Insert(stf.Item1);
                if (stf.Item2 != null)
                {
                    stf.Item2.StfId = stf_id;
                    _TUserDao.Insert(stf.Item2);
                }
                _TStaffDao.DASession.CommitTransaction();
                return stf_id;
            }
            catch (System.Exception ex)
            {
                _TStaffDao.DASession.RollbackTransaction();
                return 0;
            }
        }

        public bool Update(Tuple<TStaff, TUser> stf)
        {
            _TStaffDao.DASession.BeginTransaction();
            try
            {
                bool r = _TStaffDao.Update(stf.Item1);
                Dictionary<string, object> con = new Dictionary<string, object>();
                con[TUser.STF_ID] = stf.Item1.StfId;
                r = r && (_TUserDao.Delete(con) > 0);
                if (stf.Item2 != null)
                {
                    stf.Item2.StfId = stf.Item1.StfId;
                    _TUserDao.Insert(stf.Item2);
                }
                _TStaffDao.DASession.CommitTransaction();
                return r;
            }
            catch (System.Exception ex)
            {
                _TStaffDao.DASession.RollbackTransaction();
                return false;
            }
        }

        public bool Delete(int stf_id)
        {
            _TStaffDao.DASession.BeginTransaction();
            try
            {
                Dictionary<string, object> con = new Dictionary<string, object>();
                con[TUser.STF_ID] = stf_id;

                bool r = _TStaffDao.Delete(stf_id) > 0;
                r = r && _TUserDao.Delete(con) > 0;
                _TStaffDao.DASession.CommitTransaction();
                return r;
            }
            catch (System.Exception ex)
            {
                _TStaffDao.DASession.RollbackTransaction();
                return false;
            }
        }

        public bool Deactive(int stf_id)
        {
            _TStaffDao.DASession.BeginTransaction();
            try
            {
                TStaff staff = _TStaffDao.Get(stf_id);
                Dictionary<string, object> con = new Dictionary<string, object>();
                con[TUser.STF_ID] = staff.StfId;
                TUser user = _TUserDao.Count(con) > 0 ? _TUserDao.List(con, 1, 1)[0] : null;
                bool r = false;
                if (staff != null)
                {
                    staff.IsActive = 0;
                    r = _TStaffDao.Update(staff);
                }
                if (user != null)
                {
                    user.LoginAvalible = 0;
                    r = r && _TUserDao.Update(user);
                }
                _TStaffDao.DASession.CommitTransaction();
                return r;
            }
            catch (System.Exception ex)
            {
                _TStaffDao.DASession.RollbackTransaction();
                return false;
            }
        }

        public bool Active(int stf_id)
        {
            _TStaffDao.DASession.BeginTransaction();
            try
            {
                TStaff staff = _TStaffDao.Get(stf_id);
                Dictionary<string, object> con = new Dictionary<string, object>();
                con[TUser.STF_ID] = staff.StfId;
                TUser user = _TUserDao.Count(con) > 0 ? _TUserDao.List(con, 1, 1)[0] : null;
                bool r = false;
                if (staff != null)
                {
                    staff.IsActive = 1;
                    r = _TStaffDao.Update(staff);
                }
                if (user != null)
                {
                    user.LoginAvalible = 1;
                    r = r && _TUserDao.Update(user);
                }
                _TStaffDao.DASession.CommitTransaction();
                return r;
            }
            catch (System.Exception ex)
            {
                _TStaffDao.DASession.RollbackTransaction();
                return false;
            }
        }

        public int Count(IDictionary con)
        {
            return _TStaffDao.Count(con);
        }

        public IList<Tuple<TStaff, TUser>> List(IDictionary con, int pagesize, int page)
        {
            IList<TStaff> stf_list = _TStaffDao.List(con, pagesize, page);
            if (stf_list == null) return null;
            IList<Tuple<TStaff, TUser>> list = new List<Tuple<TStaff, TUser>>();
            Dictionary<string, object> cond = new Dictionary<string, object>();
            foreach (var stf in stf_list)
            {
                cond[TUser.STF_ID] = stf.StfId;
                TUser user = _TUserDao.Count(cond) > 0 ? _TUserDao.List(cond)[0] : null;
                list.Add(new Tuple<TStaff, TUser>(stf, user));
            }
            return list;
        }

        public IList<Tuple<TStaff, TUser>> ListAll()
        {
            IList<TStaff> stf_list = _TStaffDao.List(null);
            if (stf_list == null) return null;
            IList<Tuple<TStaff, TUser>> list = new List<Tuple<TStaff, TUser>>();
            Dictionary<string, object> cond = new Dictionary<string, object>();
            foreach (var stf in stf_list)
            {
                cond[TUser.STF_ID] = stf.StfId;
                TUser user = _TUserDao.Count(cond) > 0 ? _TUserDao.List(cond)[0] : null;
                list.Add(new Tuple<TStaff, TUser>(stf, user));
            }
            return list;
        }
    }
}
